package com.lightbook.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lightbook.pojo.Cart;
import com.lightbook.result.Result;
import com.lightbook.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "购物车管理")
@Controller
@RequestMapping("/adminCart")
public class CartAdminController {
    @Autowired
    private CartService cartService;

    @GetMapping("")
    public String cart() {
        return "cart2";
    }

    @ResponseBody
    @GetMapping("/jsontest")
    public List<Cart> jsonTest() {

        List<Cart> list = cartService.list(null);
        return list;
    }

    //    条件分页查询
//    page 当前页 limit每页显示条数
    @ResponseBody
    @GetMapping("/table/{page}/{limit}")
    public Map<String, Object> dynamic_table(@PathVariable Long page, @PathVariable Long limit){
        //查询所有数据
        // List<User> list = userService.list();
        //model.addAttribute("users",list);
        //分页查询数据
        Page<Cart> cartPage = new Page<Cart>(page, limit);//pn是当前页码，5为每页显示的数据条数
        Page<Cart> pages= cartService.page(cartPage, new QueryWrapper<Cart>().orderByDesc("cart_id"));//分页查询的结果
        List<Cart> list = cartService.list();
//        String cart1 = pages.getRecords().get(6).getUserEmail();

        //        商品记录数
        Integer cartTotal1=0;
//        商品总价格
        double cartTotal2=0;

        for(Cart cart:list)
        {
            cartTotal1+=cart.getCartCount();
            cartTotal2+=cart.getCartTotalPrice();
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("cartTotal1",cartTotal1);
        map.put("cartTotal2",cartTotal2);
        map.put("pages",pages);
        return map;
    }


    //Admin部分
    @ApiOperation("根据id查询订单")
    @ResponseBody
    @GetMapping("/get/{id}")
    public Cart getById(@PathVariable String id) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cart_id", id);
        return cartService.list(queryWrapper).get(0);
    }

    @ApiOperation("根据id修改订单")
    @ResponseBody
    @PutMapping("/updateById")
    public Boolean updateById(@RequestBody Cart cart) {
        return cartService.saveOrUpdate(cart);
    }

    @ApiOperation("根据id删除订单")
    @ResponseBody
    @DeleteMapping("/removeById/{id}")
    public Boolean removeById(@PathVariable String id){
        return cartService.removeById(id);
    }

    @ApiOperation("分页查询")
    @ResponseBody
    @GetMapping("/search/{page}/{limit}")
    public Result getListByKeyword(@PathVariable Long page,
                                   @PathVariable Long limit,
                                   String keyword) {
        Page<Cart> pageNum = new Page<>(page, limit);
        IPage<Cart> iPage = cartService.selectPage(pageNum, keyword);
        return Result.ok(iPage);
    }

    //批量删除
    @ApiOperation("批量删除")
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        cartService.removeByIds(idList);
        return Result.ok();
    }
}
